cd "~/Wilfahrt_APSR Replication Data"

*** MERGE AFROBAROMETER DATA
* ROUND 9
{
import delimited "Data/Mechanisms/AB Data/afb_full_r9.csv", varnames(1) clear

*personal covariates
gen Female = 0
replace Female = 1 if q100 == 2
rename q1 respAge
replace respAge = . if respAge > 105 | respAge == -1
rename q94 Education
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == 1

*Traditional leaders
rename q38h TradLead_Corrupt
replace TradLead_Corrupt = . if TradLead_Corrupt == -1 | TradLead_Corrupt > 7
rename q37j TradLead_Trust
replace TradLead_Trust = . if TradLead_Trust == -1 | TradLead_Trust > 7
rename q11e TradLead_Contact
replace TradLead_Contact = . if TradLead_Contact == -1 | TradLead_Contact > 7

* Vote pressure
rename q9c Vote_Pressure
replace Vote_Pressure = . if Vote_Pressure == -1 | Vote_Pressure > 7

drop country
rename country_r5list country
label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria"  25 "Senegal" 26 "Sierra Leone" 27 "South Africa" 28 "Sudan" 29 "eSwatini" 30 "Tanzania" 31 "Togo" 32 "Tunisia" 33 "Uganda" 34 "Zambia" 35 "Zimbabwe" 36 "Gabon" 37 "Gambia" 38 "Sao Tome and Principe" 39 "Angola" 40 "Ethiopia" 41 "Mauritania" 42 "Seychelles" 43 "Congo-Brazzaville"
label values country Country

gen Round = 9

*Asset index
gen radio = q90a
gen tv = q90b
gen car = q90c

foreach var of varlist q6a q6b q6c q6d q6e radio tv car q91a {
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == 9
	replace `var' = . if `var' == 98
	}
	
gen water = .
replace water = 1 if q91a == 1 | q91a == 2 
replace water = 0 if q91a> 2

gen Poverty = (q6a+q6b+q6c+q6d+q6e)/5
gen assets = (radio+tv+car+water)/4

factor radio tv car water q6a q6b q6c q6d q6e, ipf factor(1) 
predict poverty2

rename eanumb uniqueea
rename ea_gps_la latitude
rename ea_gps_lo longitude

drop if  latitude == .
replace uniqueea = eanumb_ab if uniqueea == .

gen year2 = substr(dateintr,1, strpos(dateintr, "-") - 1)
gen year = real(year2)

keep  year  Vote_Pressure  poverty2 Poverty country uniqueea Round Urban Education respAge Female respno latitude longitude   TradLead_Corrupt TradLead_Trust TradLead_Contact 

save "Data/Mechanisms/AB Data/afb_cleaned_rd9.dta", replace
}

* ROUND 8
{
import delimited "Data/Mechanisms/AB Data/afb_full_r8.csv", varnames(1) clear

*personal covariates
gen Female = 0
replace Female = 1 if q101 == 2
rename q1 respAge
replace respAge = . if respAge > 105 | respAge == -1
rename q97 Education
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == 1

*Traditional leaders 
rename q87a TradLead_InfluenceLG
replace TradLead_InfluenceLG = . if TradLead_InfluenceLG == -1 | TradLead_InfluenceLG > 7
rename q87b TradLead_Influence_Land
replace TradLead_Influence_Land = . if TradLead_Influence_Land == -1 | TradLead_Influence_Land > 7
rename q87c TradLead_Influence_Vote
replace TradLead_Influence_Vote = . if TradLead_Influence_Vote == -1 | TradLead_Influence_Vote > 7
rename q87d TradLead_Influence_Disputes
replace TradLead_Influence_Disputes = . if TradLead_Influence_Disputes == -1 | TradLead_Influence_Disputes > 7
rename q87e TradLead_MoreInfluence
replace TradLead_MoreInfluence = . if TradLead_MoreInfluence == -1 | TradLead_MoreInfluence > 7
rename q12d TradLead_Contact
replace TradLead_Contact = . if TradLead_Contact == -1 | TradLead_Contact > 7
rename q38c TradLead_Listen
replace TradLead_Listen = . if TradLead_Listen == -1 | TradLead_Listen > 7
rename q41k TradLead_Trust
replace TradLead_Trust = . if TradLead_Trust == -1 | TradLead_Trust > 7
rename q42h TradLead_Corrupt
replace TradLead_Corrupt = . if TradLead_Corrupt == -1 | TradLead_Corrupt > 7
rename q51d TradLead_Performance
replace TradLead_Performance = . if TradLead_Performance == -1 | TradLead_Performance > 6

gen TradLead_StayOut_Politics = .
replace TradLead_StayOut_Politics = 1 if q89a == 1
replace TradLead_StayOut_Politics = 2 if q89a == 2
replace TradLead_StayOut_Politics = 3 if q89a == 5
replace TradLead_StayOut_Politics = 4 if q89a == 3
replace TradLead_StayOut_Politics = 5 if q89a ==4
gen TradLead_Coop_Elected = .
replace TradLead_Coop_Elected = 1 if q89b == 1
replace TradLead_Coop_Elected = 2 if q89b == 2
replace TradLead_Coop_Elected = 3 if q89b == 5
replace TradLead_Coop_Elected = 4 if q89b == 3
replace TradLead_Coop_Elected = 5 if q89b ==4

* Ethnic Id
rename q86b Ethnic_Neighbors
replace Ethnic_Neighbors = . if Ethnic_Neighbors == -1 | Ethnic_Neighbors > 6
rename q86a Religion_Neighbors
replace Religion_Neighbors = . if Religion_Neighbors == -1 | Religion_Neighbors > 6
rename q86e Party_Neighbors
replace Party_Neighbors = . if Party_Neighbors == -1 | Party_Neighbors > 6
rename q86d Immigrant_Neighbors
replace Immigrant_Neighbors = . if Immigrant_Neighbors == -1 | Immigrant_Neighbors > 6

* Vote pressure
rename q10c Vote_Pressure
replace Vote_Pressure = . if Vote_Pressure == -1 | Vote_Pressure > 7

drop country 
rename country_r5list country
label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria"  25 "Senegal" 26 "Sierra Leone" 27 "South Africa" 28 "Sudan" 29 "eSwatini" 30 "Tanzania" 31 "Togo" 32 "Tunisia" 33 "Uganda" 34 "Zambia" 35 "Zimbabwe" 36 "Gabon" 37 "Gambia" 38 "Sao Tome and Principe" 39 "Angola" 40 "Ethiopia" 41 "Mauritania" 42 "Seychelles" 43 "Congo-Brazzaville"
label values country Country

gen Round = 8

*Asset index
gen radio = q92a
gen tv = q92b
gen car = q92c

foreach var of varlist q7a q7b q7c q7d q7e radio tv car q93a {
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == 9
	replace `var' = . if `var' == 98
	}
	
gen water = .
replace water = 1 if q93a == 1 | q93a == 2 
replace water = 0 if q93a >2

gen Poverty = (q7a+q7b+q7c+q7d+q7e)/5
gen assets = (radio+tv+car+water)/4

factor radio tv car water q7a q7b q7c q7d q7e, ipf factor(1) 
predict poverty2

rename eanumb uniqueea
rename ea_gps_la latitude
rename ea_gps_lo longitude

drop if  latitude == .
replace uniqueea = eanumb_ab if uniqueea == .

gen year = substr(dateintr,1, strpos(dateintr, "-") - 1)
gen year2 = real(year)
drop year
rename year2 year

keep Vote_Pressure year Religion_Neighbors Party_Neighbors Immigrant_Neighbors Ethnic_Neighbors TradLead_InfluenceLG TradLead_MoreInfluence TradLead_Influence_Disputes TradLead_Influence_Vote TradLead_Influence_Land TradLead_Performance TradLead_StayOut_Politics TradLead_Coop_Elected poverty2 Poverty country uniqueea Round Urban Education respAge Female respno latitude longitude   TradLead_Corrupt TradLead_Trust TradLead_Contact 

save "Data/Mechanisms/AB Data/afb_cleaned_rd8.dta", replace
}

* ROUND 7
{
import delimited "Data/Mechanisms/AB Data/R7.Merge_34ctry.4Dec18.final_Updated.13May24.release.w.local.info.csv", clear

*personal covariates
gen Female = 0
replace Female = 1 if q101 == 2
rename q1 respAge
replace respAge = . if respAge > 108 | respAge == -1
rename q97 Education
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == 1

*Traditional leaders
rename q44g TradLead_Corrupt
replace TradLead_Corrupt = . if TradLead_Corrupt == -1 | TradLead_Corrupt > 7
rename q43j TradLead_Trust
replace TradLead_Trust = . if TradLead_Trust == -1 | TradLead_Trust > 7
rename q25d TradLead_Contact
replace TradLead_Contact = . if TradLead_Contact == -1 | TradLead_Contact > 7
rename q58d TradLead_Performance
replace TradLead_Performance = . if TradLead_Performance == -1 | TradLead_Performance > 6

* Ethnicity
rename q87b Ethnic_Neighbors
replace Ethnic_Neighbors = . if Ethnic_Neighbors == -1 | Ethnic_Neighbors > 6
rename q87a Religion_Neighbors
replace Religion_Neighbors = . if Religion_Neighbors == -1 | Religion_Neighbors > 6
rename q87d Immigrant_Neighbors
replace Immigrant_Neighbors = . if Immigrant_Neighbors == -1 | Immigrant_Neighbors > 6

label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria" 25 "Senegal" 26 "Sierra Leone" 27 "South Africa" 28 "Sudan" 29 "eSwatini" 30 "Tanzania" 31 "Togo" 32 "Tunisia" 33 "Uganda" 34 "Zambia" 35 "Zimbabwe" 36 "Gabon" 37 "Gambia" 38 "Sao Tome and Principe"
label values country_r5list Country

drop country
rename country_r5list country

gen Round = 7

*Asset index
gen radio = q89a
gen tv = q89b
gen car = q89b

foreach var of varlist q8a q8b q8c q8d q8e radio tv car q92a {
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == 9
	replace `var' = . if `var' == 98
	}
	
gen water = .
replace water = 1 if q92a == 1 | q92a == 2 
replace water = 0 if q92a == 3

gen Poverty = (q8a+q8b+q8c+q8d+q8e)/5
gen assets = (radio+tv+car+water)/4

factor radio tv car water q8a q8b q8c q8d q8e, ipf factor(1) 
predict poverty2

gen latitude = real(ea_gps_la)
gen longitude = real(ea_gps_lo)

drop if  latitude == .
gen uniqueea = eanumb_ab 

gen year2 = substr(dateintr,7, strpos(dateintr, "-"))
gen year = 2016 if year2 == "-16" | year2 == "16"
replace year = 2017 if year2 == "-17" | year2 == "17"
replace year = 2018 if year2 == "-18" | year2 == "18"

keep  Religion_Neighbors Immigrant_Neighbors year Ethnic_Neighbors  poverty2 Poverty country uniqueea Round  Urban Education respAge Female respno latitude longitude   TradLead_Corrupt TradLead_Trust TradLead_Contact TradLead_Performance

save "Data/Mechanisms/AB Data/afb_cleaned_rd7.dta", replace
}

* ROUND 6
{
import delimited "Data/Mechanisms/AB Data/afb_full_r6.csv", clear

*personal covariates
gen Female = 0
replace Female = 1 if thisint == 2
rename q1 respAge
replace respAge = . if respAge > 105 | respAge == -1
rename q97 Education
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == 1

*ethnicity
rename q89b Ethnic_Neighbors
replace Ethnic_Neighbors = . if Ethnic_Neighbors == -1 | Ethnic_Neighbors > 6
rename q89a Religion_Neighbors
replace Religion_Neighbors = . if Religion_Neighbors == -1 | Religion_Neighbors > 6
rename q89e Immigrant_Neighbors
replace Immigrant_Neighbors = . if Immigrant_Neighbors == -1 | Immigrant_Neighbors > 6

*TAs
rename q53h TradLead_Corrupt
replace TradLead_Corrupt = . if TradLead_Corrupt == -1 | TradLead_Corrupt > 8
rename q52k TradLead_Trust
replace TradLead_Trust = . if TradLead_Trust == -1 | TradLead_Trust > 8
rename q24e TradLead_Contact
replace TradLead_Contact = . if TradLead_Contact == -1 | TradLead_Contact > 8
rename q68d TradLead_Performance
replace TradLead_Performance = . if TradLead_Performance == -1 | TradLead_Performance > 6

* Vote pressure
rename q15c Vote_Pressure
replace Vote_Pressure = . if Vote_Pressure == -1 | Vote_Pressure > 8

replace country_r5list = 38 if country_r5list == 37
label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria" 25 "Senegal" 26 "Sierra Leone" 27 "South Africa" 28 "Sudan" 29 "eSwatini" 30 "Tanzania" 31 "Togo" 32 "Tunisia" 33 "Uganda" 34 "Zambia" 35 "Zimbabwe" 36 "Gabon" 38 "Sao Tome and Principe"

label values country_r5list Country

gen Round = 6

*Asset index
gen radio = q91a
gen tv = q91b
gen car = q91c

foreach var of varlist q8a q8b q8c q8d q8e radio tv car q93a {
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == 9
	replace `var' = . if `var' == 98
	}
	
gen water = .
replace water = 1 if q93a == 1 | q93a == 2 
replace water = 0 if q93a == 3

gen Poverty = (q8b+q8a+q8c+q8d+q8e)/5
gen assets = (radio+tv+car+water)/4

factor radio tv car water q8a q8b q8c q8d q8e , ipf factor(1) 
predict poverty2

drop country
rename country_r5list country

gen year2 = substr(dateintr,7, strpos(dateintr, "-"))
gen year = 2014 if year2 == "-14" | year2 == "14"
replace year = 2015 if year2 == "-15" | year2 == "15"

keep Immigrant_Neighbors  Religion_Neighbors year  Ethnic_Neighbors Vote_Pressure poverty2 Poverty country uniqueea Round  Urban Education respAge Female respno latitude longitude  TradLead_Corrupt TradLead_Trust TradLead_Contact TradLead_Performance 

save "Data/Mechanisms/AB Data/afb_cleaned_rd6.dta", replace
}

* ROUND 5
{
import delimited "Data/Mechanisms/AB Data/afb_full_r5_34ctry.csv", clear

*personal covariates
gen Female = 0
replace Female = 1 if thisint == 2
rename q1 respAge
replace respAge = . if respAge > 103 | respAge == -1
rename q97 Education
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == 1

*Violence
gen Violence_justified = .
replace Violence_justified =1 if q78 == 1
replace Violence_justified =2 if q78 == 2
replace Violence_justified =3 if q78 == 5
replace Violence_justified =4 if q78 == 3
replace Violence_justified =5 if q78 == 4
rename q26e Used_Pol_Violence
replace Used_Pol_Violence = . if Used_Pol_Violence == -1 | Used_Pol_Violence > 7

*Equality
gen Leaders_Equal_v_Grp = .
replace Leaders_Equal_v_Grp =1 if q18 == 4
replace Leaders_Equal_v_Grp =2 if q18 == 3
replace Leaders_Equal_v_Grp =3 if q18 == 5
replace Leaders_Equal_v_Grp =4 if q18 == 2
replace Leaders_Equal_v_Grp =5 if q18 == 1

* Vote pressure
rename q17c Vote_Pressure
replace Vote_Pressure = . if Vote_Pressure == -1 | Vote_Pressure > 8

drop country
rename country_alpha country

label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria" 25 "Senegal" 26 "Sierra Leone" 27 "South Africa" 28 "Sudan" 29 "eSwatini" 30 "Tanzania" 31 "Togo" 32 "Tunisia" 33 "Uganda" 34 "Zambia" 35 "Zimbabwe" 
label values country Country

gen Round = 5

gen radio = q90a
gen tv = q90b
gen car = q90c

foreach var of varlist q8a q8b q8c q8d q8e radio tv car q95a {
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == 9
	replace `var' = . if `var' == 98
	}
	
gen water = .
replace water = 1 if q95a == 1 | q95a == 2 
replace water = 0 if q95a == 3

gen Poverty = (q8b+q8a+q8c+q8d+q8e)/5

factor radio tv car water q8a q8b q8c q8d q8e , ipf factor(1) 
predict poverty2

gen year2 = substr(dateintr,7, strpos(dateintr, "-"))
gen year = 2011 if year2 == "-11" 
replace year = 2012 if year2 == "-12"
replace year = 2013 if year2 == "-13"

keep  Leaders_Equal_v_Grp year Violence_justified Used_Pol_Violence Vote_Pressure uniqueea poverty2 Poverty country Round   Urban Education respAge Female respno latitude longitude  

save "Data/Mechanisms/AB Data/afb_cleaned_rd5.dta", replace
}

* ROUND 4
{
import delimited "Data/Mechanisms/AB Data/afb_full_r4.csv", clear

*personel covariates
gen Female = 0
replace Female = 1 if thisint == 2
rename q1 respAge
replace respAge = . if respAge > 110 | respAge == -1
rename q89 Education
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == 1

*Equality
gen Leaders_Equal_v_Grp = .
replace Leaders_Equal_v_Grp =1 if q17 == 1
replace Leaders_Equal_v_Grp =2 if q17 == 2
replace Leaders_Equal_v_Grp =3 if q17 == 5
replace Leaders_Equal_v_Grp =4 if q17 == 3
replace Leaders_Equal_v_Grp =5 if q17 == 4

* Vote pressure
rename q15c Vote_Pressure
replace Vote_Pressure = . if Vote_Pressure == -1 | Vote_Pressure > 8

* Traditional leaders
rename q50h TradLead_Corrupt
replace TradLead_Corrupt = . if TradLead_Corrupt == -1 | TradLead_Corrupt > 8
rename q49i TradLead_Trust
replace TradLead_Trust = . if TradLead_Trust == -1 | TradLead_Trust > 8
rename q27b TradLead_Contact
replace TradLead_Contact = . if TradLead_Contact == -1 | TradLead_Contact > 8
rename q54c TradLead_Listen
replace TradLead_Listen = . if TradLead_Listen == -1 | TradLead_Listen > 8
rename q65 TradLead_InfluenceLG
replace TradLead_InfluenceLG = . if TradLead_InfluenceLG == -1 | TradLead_InfluenceLG > 8
rename q66 TradLead_MoreInfluence
replace TradLead_MoreInfluence = . if TradLead_MoreInfluence == -1 | TradLead_MoreInfluence > 8
rename q67 TradLead_SitLG
replace TradLead_SitLG = . if TradLead_SitLG == -1 | TradLead_SitLG > 8
gen TradLead_ManageLand = 0
replace TradLead_ManageLand = 1 if q58f == 3
replace TradLead_ManageLand = . if q58f == -1 | q58f > 8
gen TradLead_ResolveDisputes = 0
replace TradLead_ResolveDisputes = 1 if q58e == 3
replace TradLead_ResolveDisputes = . if q58e == -1 | q58e > 8
gen TradLead_LawOrder = 0
replace TradLead_LawOrder = 1 if q58h == 3
replace TradLead_LawOrder = . if q58h == -1 | q58h > 8

gen Round = 4

rename country country_old
gen country = .
replace country = 2 if country_old ==1
replace country = 3 if country_old ==2
replace country = 4 if country_old ==3
replace country = 7 if country_old ==4
replace country = 11 if country_old ==5
replace country = 13 if country_old ==6
replace country = 14 if country_old ==7
replace country = 15 if country_old ==8
replace country = 16 if country_old ==9
replace country = 17 if country_old ==10
replace country = 18 if country_old ==11
replace country = 21 if country_old ==12
replace country = 22 if country_old ==13
replace country = 24 if country_old ==14
replace country = 25 if country_old ==15
replace country = 27 if country_old ==16
replace country = 30 if country_old ==17
replace country = 33 if country_old ==18
replace country = 34 if country_old ==19
replace country = 35 if country_old ==20

label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 10 "Gabon" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria" 25 "Sao Tome" 26 "Senegal" 27 "Sierra Leone" 28 "South Africa" 29 "Sudan" 30 "eSwatini" 31 "Tanzania" 32 "Togo"  33 "Uganda" 34 "Zambia" 35 "Zimbabwe"
label values country Country

gen radio = q92a
gen tv = q92b
gen car = q92c

foreach var of varlist q8a q8b q8c q8d q8e radio tv car q93a {
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == 9
	replace `var' = . if `var' == 98
	}
	
gen water = .
replace water = 1 if q93a == 1 | q93a == 2 
replace water = 0 if q93a == 3

gen Poverty = (q8b+q8a+q8c+q8d+q8e)/5

factor radio tv car water q8a q8b q8c q8d q8e , ipf factor(1) 
predict poverty2

gen year2 = substr(dateintr,7, strpos(dateintr, "-"))
gen year = 2008 if year2 == "-08" | year2 == "08" 
replace year = 2009 if year2 == "-09" | year2 == "09" 

keep Leaders_Equal_v_Grp year uniqueea TradLead_ManageLand TradLead_ResolveDisputes TradLead_LawOrder Vote_Pressure poverty2 Poverty country TradLead_MoreInfluence TradLead_SitLG TradLead_InfluenceLG Round Urban Education respAge Female respno latitude longitude TradLead_Corrupt TradLead_Trust TradLead_Contact TradLead_Listen 

save "Data/Mechanisms/AB Data/afb_cleaned_rd4.dta", replace
}

* ROUND 3
{
import delimited "Data/Mechanisms/AB Data/afb_full_r3.csv", clear

* personel covariates
gen Female = 0
replace Female = 1 if currint == 2
rename q1 respAge
replace respAge = . if respAge > 105 | respAge == -1
rename q90 Education
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == 1

*Equality
gen Leaders_Equal_v_Grp = .
replace Leaders_Equal_v_Grp =1 if q21 == "1"
replace Leaders_Equal_v_Grp =2 if q21 == "2"
replace Leaders_Equal_v_Grp =3 if q21 == "5"
replace Leaders_Equal_v_Grp =4 if q21 == "3"
replace Leaders_Equal_v_Grp =5 if q21 == "4"

*Violence
gen Violence_justified = .
replace Violence_justified =1 if q51 == 1
replace Violence_justified =2 if q51 == 2
replace Violence_justified =3 if q51 == 5
replace Violence_justified =4 if q51 == 3
replace Violence_justified =5 if q51 == 4

* Traditional leaders
gen TradLead_Contact = 0
replace TradLead_Contact =1 if q32f == "1"
replace TradLead_Contact =2 if q32f == "2"
replace TradLead_Contact =3 if q32f == "3"
replace TradLead_Contact =. if q32f == "-1" | q32f == "#NULL!" | q32f == "7" | q32f == "9"

gen Round = 3

rename country country_old
gen country = .
replace country = 2 if country_old ==1
replace country = 3 if country_old ==2
replace country = 7 if country_old ==3
replace country = 11 if country_old ==4
replace country = 13 if country_old ==5
replace country = 14 if country_old ==6
replace country = 16 if country_old ==7
replace country = 17 if country_old ==8
replace country = 18 if country_old ==9
replace country = 21 if country_old ==10
replace country = 22 if country_old ==11
replace country = 24 if country_old ==12
replace country = 25 if country_old ==13
replace country = 27 if country_old ==14
replace country = 30 if country_old ==15
replace country = 33 if country_old ==16
replace country = 34 if country_old ==17
replace country = 35 if country_old ==18

label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 10 "Gabon" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria" 25 "Senegal" 26 "Sierra Leone" 27 "South Africa" 28 "Sudan" 29 "eSwatini" 30 "Tanzania" 31 "Togo" 33  "Uganda" 34"Zambia" 35 "Zimbabwe"
label values country Country

replace uniqueea = uniqueea*3000

gen radio = q93b
gen tv = q93c
gen car = q93f
replace car = 1 if q93e == 1

foreach var of varlist q8a q8b q8c q8d q8e radio tv car q93a {
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == 9
	replace `var' = . if `var' == 98
	replace `var' = . if `var' == 998
	}
	
gen Poverty = (q8b+q8a+q8c+q8d+q8e)/5

factor radio tv car q8a q8b q8c q8d q8e , ipf factor(1) 
predict poverty2

gen year2 = substr(dateintr,7, strpos(dateintr, "-"))
gen year = 2005 if year2 == "-05" | year2 == "05" 
replace year = 2006 if year2 == "-06" | year2 == "06" 

keep Leaders_Equal_v_Grp year Violence_justified  uniqueea poverty2 Poverty country Round Urban Education respAge Female respno latitude longitude  TradLead_Contact  

save "Data/Mechanisms/AB Data/afb_cleaned_rd3.dta", replace
}

* ROUND 2
{
import delimited "Data/Mechanisms/AB Data/afb_full_r2.csv", clear

* personel covariates
gen Female = 0
replace Female = 1 if currint == "2"
replace q80 = "" if q80 == "#NULL!"
gen respAge = real(q80)
replace respAge = . if respAge > 105 | respAge == -1
replace q84 = "" if q84 == "#NULL!"
gen Education = real(q84)
replace Education = . if Education > 9 |  Education == -1
gen Urban = 0
replace Urban = 1 if urbrur == "1"

*Equality
gen Leaders_Equal_v_Grp = .
replace Leaders_Equal_v_Grp =1 if q69 == "1"
replace Leaders_Equal_v_Grp =2 if q69 == "2"
replace Leaders_Equal_v_Grp =3 if q69 == "5"
replace Leaders_Equal_v_Grp =4 if q69 == "3"
replace Leaders_Equal_v_Grp =5 if q69 == "4"

*Violence
gen Violence_justified = .
replace Violence_justified =1 if q76 == "1"
replace Violence_justified =2 if q76 == "2"
replace Violence_justified =3 if q76 == "5"
replace Violence_justified =4 if q76 == "3"
replace Violence_justified =5 if q76 == "4"
replace q25e = "" if q25e == "#NULL!"
gen Used_Pol_Violence = real(q25e)
replace Used_Pol_Violence = . if Used_Pol_Violence == -1 | Used_Pol_Violence >7

* Traditional leaders
replace q43k = "" if q43k == "#NULL!"
gen TradLead_Trust = real(q43k)
replace TradLead_Trust = . if TradLead_Trust == -1 | TradLead_Trust > 8
gen TradLead_Contact = 0
replace TradLead_Contact =1 if q29f == "1"
replace TradLead_Contact =2 if q29f == "2"
replace TradLead_Contact =3 if q29f == "3"
replace TradLead_Contact =. if q29f == "-1" | q29f == "#NULL!" | q29f == "9" | q29f == "97"

gen Round = 2

rename country country_old
gen country = .
replace country = 3 if country_old =="1"
replace country = 11 if country_old =="2"
replace country = 14 if country_old =="3"
replace country = 17 if country_old =="4"
replace country = 18 if country_old =="5"
replace country = 22 if country_old =="6"
replace country = 24 if country_old =="7"
replace country = 27 if country_old =="8"
replace country = 30 if country_old =="9"
replace country = 33 if country_old =="10"
replace country = 34 if country_old =="11"
replace country = 35 if country_old =="12"
replace country = 7 if country_old =="13"
replace country = 13 if country_old =="14"
replace country = 21 if country_old =="15"
replace country = 25 if country_old =="16"

label define Country 1 "Algeria" 2 "Benin" 3 "Botswana" 4 "Burkina Faso" 5 "Burundi" 6 "Cameroon" 7 "Cape Verde" 8 "Cote d'Ivoire" 9 "Egypt" 10 "Gabon" 11 "Ghana" 12 "Guinea" 13 "Kenya" 14 "Lesotho" 15 "Liberia" 16 "Madagascar" 17 "Malawi" 18 "Mali" 19 "Mauritius" 20 "Morocco" 21 "Mozambique" 22 "Namibia" 23 "Niger" 24 "Nigeria" 25 "Senegal" 26 "Sierra Leone" 27 "South Africa" 28 "Sudan" 29 "eSwatini" 30 "Tanzania" 32 "Togo" 33  "Uganda" 34 "Zambia" 35 "Zimbabwe"
label values country Country

replace uniqueea = uniqueea*2000

foreach var of varlist q9a q9b q9c q9e q9f  {
	replace `var' = "" if `var' == "#NULL!"
	gen `var'_2 = real(`var')
	replace `var'_2 = . if `var'_2  == -1
	replace `var'_2 = . if `var'_2 == 9
	replace `var'_2 = . if `var'_2 == 98
	replace `var'_2 = . if `var'_2 == 998
	}

gen Poverty = (q9b_2+q9a_2+q9c_2+q9f_2+q9e_2)/5

gen year2 = substr(dateintr,7, strpos(dateintr, "-"))
gen year = 2002 if year2 == "-02" | year2 == "02" 
replace year = 2003 if year2 == "-03" | year2 == "03" 
replace year = 2004 if year2 == "-04" | year2 == "04" 

keep year Leaders_Equal_v_Grp Violence_justified Used_Pol_Violence  TradLead_Trust  uniqueea Poverty country Round Urban Education respAge Female respno latitude longitude     TradLead_Contact  

save "Data/Mechanisms/AB Data/afb_cleaned_rd2.dta", replace
}


use  "Data/Mechanisms/AB Data/afb_cleaned_rd9.dta", clear
append using "Data/Mechanisms/AB Data/afb_cleaned_rd8.dta"
append using "Data/Mechanisms/AB Data/afb_cleaned_rd7.dta"
append using "Data/Mechanisms/AB Data/afb_cleaned_rd6.dta"
append using "Data/Mechanisms/AB Data/afb_cleaned_rd5.dta"
append using  "Data/Mechanisms/AB Data/afb_cleaned_rd4.dta"
append using "Data/Mechanisms/AB Data/afb_cleaned_rd3.dta"
append using "Data/Mechanisms/AB Data/afb_cleaned_rd2.dta"

drop if country == 19 | country == 20 | country == 32 | country == 10 | country == 9 | country == 7 | country == 1 | country == 42 | country == 38

gen Respondent_N = _n

sort Respondent_N
save "Data/Mechanisms/AB Data/afb_cleaned_ALL ROUNDS.dta", replace

drop if latitude == .
keep uniqueea respno latitude longitude Round Respondent_N
export delimited using "Data/Mechanisms/AB_Coordinates.csv", replace


import delimited "Data/Calculated Output/AB_Cluster_Match.csv", varnames(1) clear 
keep grid_id uniqueea round respno respondent_n
rename respondent_n Respondent_N
rename round Round
sort Respondent_N
save "Data/Calculated Output/AB_Grid_ID.dta", replace


*************** INDIVIDUAL DATA
import delimited "Data/Mechanisms/AB_D_Capitals.csv", clear
rename targetid Matched_Capital
rename inputid Respondent_N
sort Respondent_N
merge Respondent_N using "Data/Mechanisms/AB Data/afb_cleaned_ALL ROUNDS.dta"

replace Matched_Capital = "Swaziland" if Matched_Capital == "eSwatini"
replace Matched_Capital = "Cote d'Ivoire" if Matched_Capital == "Ivory Coast"
replace Matched_Capital = "Congo-Brazzaville" if Matched_Capital == "Congo (Brazzaville)"

decode country, gen(country2)

gen Match = 0
replace Match = 1  if country2 != Matched_Capital
drop if Match == 1

encode distance, gen(D_Capital_AB)

bysort Round uniqueea respno: egen min_dist = min(D_Capital_AB)
keep if min_dist == D_Capital_AB

drop _merge
sort  Respondent_N
merge Respondent_N using "Data/Calculated Output/AB_Grid_ID.dta"
keep if _merge == 3
drop _merge
rename country AB_country

collapse (first) Matched_Capital distance AB_country latitude longitude respAge Vote_Pressure TradLead_Contact TradLead_Trust TradLead_Corrupt Education Female Urban Poverty poverty2 year TradLead_Performance Ethnic_Unfair Religion_Neighbors Ethnic_Neighbors Immigrant_Neighbors Party_Neighbors TradLead_InfluenceLG TradLead_Influence_Land TradLead_Influence_Vote TradLead_Influence_Disputes TradLead_MoreInfluence TradLead_StayOut_Politics TradLead_Coop_Elected Used_Pol_Violence Violence_justified Leaders_Equal_v_Grp TradLead_Listen TradLead_SitLG TradLead_ManageLand TradLead_ResolveDisputes TradLead_LawOrder country2 Match D_Capital_AB min_dist grid_id, by(Round uniqueea respno)

gen ln_D_Capital_AB_km = log(1+(D_Capital_AB/1000))

gen age2 = respAge*respAge

gen country = AB_country

revrs Vote_Pressure
rename revVote_Pressure Vote_Pressure2

sort grid_id
save "Data/Mechanisms/AB_Final.dta", replace
